package org.langqiao.io.gzip;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.zip.GZIPInputStream;

public class ReadFile {
	public static void main(String[] args){
		readFile();
		
		//readGzipAndWriteFile();
	}
	
	//读文件，写入Gzip文件中
	public static void readFile(){
		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			
			br = new BufferedReader(new InputStreamReader(new FileInputStream(("D:/T_LOGISTICS.txt"))));
			bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:/T_LOGISTICS_1.txt")));
			String line = null;
			while((line=br.readLine())!=null){
				line = line.replace("to_timestamp(","");
				String[] arr = line.split(",");
				String str = "";
				//System.out.println(arr[40]);
				for(int i=0;i<40;i++){
					str = str+arr[i]+",";
				}
				
				for(int i=41;i<arr.length;i++){
					str = str+arr[i]+",";
				}
				str = str.substring(0,str.length()-1);
				//System.out.println(str);
				//System.out.println(line);
				/*String[] arr = line.split("REC_YYYY");
				arr[0] = arr[0]+"Date";
				//System.out.println(arr[1]);
				line = arr[0]+arr[1]+arr[2]+arr[3];*/
				//System.out.println(line);
				
				//System.out.println(line);
				bw.write(str);
				bw.newLine();
			}
		} catch (Exception e) {
			
		}
		finally{
			try {
				br.close();
				bw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		
	}

	//读Gzip文件，写入到.txt文件中
	public static void readGzipAndWriteFile(){
		
		FileInputStream fin = null;
		FileOutputStream fout = null;
		GZIPInputStream gzin = null;
		byte[] buff = null;
		try {
			fin = new FileInputStream(new File("D:/test/2.zip"));
			gzin = new GZIPInputStream(fin);
			fout = new FileOutputStream(new File("D:/test/3.txt"));			
			int num = 0;
			buff = new byte[1024];
			while((num = gzin.read(buff))!=-1){
				fout.write(buff, 0, num);
			}
		} catch (Exception e) {
			
		}
		finally{
			try {
				fin.close();
				fout.close();
				gzin.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		
		
	}
}
